import axios from "axios";
import store from "@/store";

const http = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 10000,
});

axios.defaults.headers["Content-Type"] = "application/json;charset=utf-8";

// 请求处理，这里可以加一些loading等操作
http.interceptors.request.use((config) => {
  if (store.getters.token) {
    config.headers.Authorization = "bearer " + store.getters.token;
  }
  return config;
});

// 响应处理，这里可以写一些数据轻加工的处理
http.interceptors.response.use(
  (response) => {
    // if (response.data.code === 302 || response.data.code === 305) {}
    return response.data;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default http;
